package com.zw.stock_sell_storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zw.stock_sell_storage.dto.*;
import com.zw.stock_sell_storage.mapper.SaleCarMapper;
import com.zw.stock_sell_storage.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.List;

@Service
public class SaleCarServiceImpl extends ServiceImpl<SaleCarMapper, SaleCarDto> implements ISaleCarService {
    @Autowired
    SaleCarMapper saleCarMapper;
    @Autowired
    ISaleInformationService iSaleInformationService;
    @Autowired
    ISaleDetailService iSaleDetailService;
    @Autowired
    IInventoryService iInventoryService;
    @Autowired
    ILogService iLogService;

    @Override
    public List<SaleCarDto> querySaleCarList(int userId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("fk_user_id",userId);
        return saleCarMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional
    public boolean addSaleCar(SaleCarDto saleCarDto) {

        saleCarMapper.insert(saleCarDto);
        return true;
    }

    @Override
    @Transactional
    public boolean updateSaleCar(SaleCarDto saleCarDto) {

        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("sale_car_id",saleCarDto.getSaleCarId());
        saleCarMapper.update(saleCarDto,updateWrapper);
        return true;
    }

    @Override
    @Transactional
    public boolean rmvSaleCarById(int saleCarId) {

        saleCarMapper.deleteById(saleCarId);
        return true;
    }

    @Override
    @Transactional
    public boolean rmvAllSaleCar(int userId) {

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("fk_user_id",userId);
        saleCarMapper.delete(queryWrapper);
        return true;
    }

    @Override
    @Transactional
    public boolean pushSaleCar(SaleInformationDto saleInformationDto,int userId) {
        List<SaleCarDto> saleCarList = querySaleCarList(userId);
        List<SaleDetailDto> saleDetailList = new ArrayList<>();
        //添加销售信息
        iSaleInformationService.addSaleInformation(saleInformationDto);
        for (SaleCarDto saleCarDto : saleCarList) {
            //根据库存
            InventoryDto inventoryDto = iInventoryService.queryInventoryByGoodsCode(saleCarDto.getGoodsCode());
            if (inventoryDto!=null) {
                if (saleCarDto.getGoodsSaleNumber()==null){
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return false;
                }
                if (inventoryDto.getGoodsNumber()<saleCarDto.getGoodsSaleNumber()){
                    return false;
                }
                inventoryDto.setGoodsNumber(inventoryDto.getGoodsNumber()-saleCarDto.getGoodsSaleNumber());
                iInventoryService.updateInventory(inventoryDto);
            }
            //销售明细集合添加
            SaleDetailDto saleDetailDto = new SaleDetailDto();
            saleDetailDto.setSaleDetailGoodsName(saleCarDto.getGoodsName());
            saleDetailDto.setSaleDetailGoodsBrand(saleCarDto.getGoodsBrand());
            saleDetailDto.setSaleDetailGoodsCode(saleCarDto.getGoodsCode());
            saleDetailDto.setSaleDetailGoodsType(saleCarDto.getGoodsType());
            saleDetailDto.setSaleDetailPrice(saleCarDto.getGoodsSalePrice());
            saleDetailDto.setSaleDetailTime(saleInformationDto.getSaleTime());
            saleDetailDto.setSaleDetailNumber(saleCarDto.getGoodsSaleNumber());
            saleDetailDto.setFkSaleId(saleInformationDto.getSaleId());
            saleDetailList.add(saleDetailDto);

        }

        //添加销售明细
        iSaleDetailService.addSaleDetail(saleDetailList);
        //清空购物车
        rmvAllSaleCar(userId);
        LogDto logDto = new LogDto();
        logDto.setLogDate(saleInformationDto.getSaleTime());
        logDto.setLogDesc(saleInformationDto.getSaleAgent()+"于"+saleInformationDto.getSaleTime()+"出库商品，销售信息id为："+saleInformationDto.getSaleId());
        iLogService.addLog(logDto);
        return true;
    }

    @Override
    public boolean updateSaleCarByGoodsCode(String goodsCode, Double goodsSalePrice) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("goods_code",goodsCode);
        SaleCarDto saleCarDto = new SaleCarDto();
        saleCarDto.setGoodsSalePrice(goodsSalePrice);
        saleCarMapper.update(saleCarDto,updateWrapper);
        return true;
    }
}
